package com.yuchen.ishop.buyer;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.Order;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 15-4-23.
 */
@Transactional
@Path("buyerOrderService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
class BuyerOrderServiceImpl implements BuyerOrderService{

    private final static Logger LOG = Logger.getLogger(BuyerOrderService.class);

    @Autowired
    private BaseDao dao;

    @GET
    @Path("get/{id}")
    public Order getById(@PathParam("id")Long id) {
        return dao.fetch(Order.class, id);
    }

    @POST
    @Path("insert")
    public Responses insert(Order order) {
        dao.insert(order);
        return ResponseUtils.success();
    }

    @POST
    @Path("update")
    public Responses update(Order order) {
        dao.updateIgnoreNull(order);
        return ResponseUtils.success();
    }

    @DELETE
    @Path("delete/{id}")
    public Responses delete(@PathParam("id") Long id) {
        dao.delete(Order.class, id);
        return ResponseUtils.success();
    }

    @GET
    @Path("getByBuyerId/{buyerId}")
    public List<Map> getByBuyerId(@PathParam("buyerId")Long buyerId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("buyerId",buyerId);
        StringBuilder sql = new StringBuilder();
        sql.append("select t1.id ,t1.create_time, t1.real_price pay, t1.order_no, t1.sub_order_no,t1.state, t2.name, t2.price ,t3.image_url,t2.id spid, ");
        sql.append("t2.seller_xiaoer_id xiao_id,t2.seller_id seller_id ");
        sql.append("from t_orders t1, t_goods t2, t_goods_imgs t3 ");
        sql.append("where t1.goods_id = t2.id and t2.id = t3.goods_id and t3.type = 1 ");
        sql.append("and t1.buyer_id = @buyerId ");
        List list = dao.querySql(sql.toString(), params);
        return list;
    }
}
